package com.gdziarmaga.webshop.web.menus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gdziarmaga.webshop.book.model.Book;
import com.gdziarmaga.webshop.book.model.BookShortInfo;
import com.gdziarmaga.webshop.book.services.BookManager;
import com.gdziarmaga.webshop.utils.filtering.BaseFilter;
import com.gdziarmaga.webshop.utils.filtering.FilterComparisonType;
import com.gdziarmaga.webshop.utils.filtering.FilterField;
import com.gdziarmaga.webshop.utils.springcontext.AppContext;
import com.gdziarmaga.webshop.web.utils.FacesUtils;

@ManagedBean(name="bookMenuBean")
@RequestScoped
public class BookMenuBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7211940700262254607L;
	private ArrayList<BookShortInfo> book_list;
	private BookManager bookManager;

	@PostConstruct
	public void init(){
		bookManager = (BookManager) AppContext.getApplicationContext().getBean("bookManager");
	}


	public ArrayList<BookShortInfo> getBook_list() {
		BaseFilter filter = new BaseFilter();
		String selectedSection = (String) FacesUtils.flashScope().get("section");
		FacesUtils.flashScope().put("section", selectedSection);
		if(selectedSection != null){
		filter.addFilterField(new FilterField<String>(selectedSection, Book.SECTION, FilterComparisonType.EQUAL));	
		}
		this.book_list = (ArrayList<BookShortInfo>) bookManager.getBooksShortInfosByFilterObject(filter);
		return book_list;
	}


	public void changeSection(ActionEvent event){
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String selectedSection =  params.get("section");
		FacesUtils.flashScope().put("section", selectedSection);
	}
	
}
